package tgc.edu.tgq.demo.web.controller;

import java.util.HashSet
;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.tgq.demo.custom.AjaxResult;
import tgc.edu.tgq.demo.custom.FileUtils;
import tgc.edu.tgq.demo.entity.Student;
import tgc.edu.tgq.demo.entity.TbClass;
import tgc.edu.tgq.demo.service.StudentService;
import tgc.edu.tgq.demo.service.TbClassService;
import tgc.edu.tgq.demo.web.form.StudentForm;




@Controller
@RequestMapping(value = "/student")
public class StudentController {

	@Autowired
	private StudentService studentService;
	@Autowired
	private TbClassService classService;
	@Autowired
	private FileUtils fileUtils;
	
	@RequestMapping(value = "/manage")
	public void manage(ModelMap modelMap) {
		List<TbClass> list = classService.findAll();
		modelMap.put("list", list);
	}
	
	@SuppressWarnings("serial")
	public Specification<Student> buildSpec(StudentForm form){
		Specification<Student> spec=new Specification<Student>() {

			@Override
			public Predicate toPredicate(Root<Student> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				Integer bjId = form.getBjId();
				if(bjId!=null) {
					rules.add(cb.equal(root.get("tbClass").get("id"), bjId));
				}
				String stuNo = form.getStuNo();
				if(StringUtils.hasText(stuNo)) {
					rules.add(cb.like(root.get("stuNo"), "%"+stuNo+"%"));
				}
				String name = form.getName();
				if(StringUtils.hasText(name)) {
					rules.add(cb.like(root.get("name"), "%"+name+"%"));
				}			
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(StudentForm form,ModelMap modelMap) {
		Specification<Student> spec = buildSpec(form);
		List<Student> list = studentService.findAll(spec);
		return list;
	}
	
	@RequestMapping(value = "/edit")
	public void edit(StudentForm form,ModelMap modelMap) {
		Integer bjId = form.getBjId();
		Integer id = form.getId();
		Student model=new Student();
		if(id!=null) {
			model=studentService.findById(id);
		}else {
			model.setTbClass(new TbClass(bjId));
		}
		modelMap.put("model", model);
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(StudentForm form) {
		Boolean flag = studentService.save(form);
		if(flag) {
			return new AjaxResult("数据保存成功");
		}
		return new AjaxResult(false, "数据保存失败");
	}
	
	@RequestMapping(value = "/search")
	public void search(StudentForm form,ModelMap modelMap) {
		modelMap.put("model", form);
	}
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(StudentForm form) {
		Integer id=form.getId();
		try {
			studentService.deleteById(id);
			return new AjaxResult("数据删除成功！");
		} catch (Exception e) {
			return new AjaxResult(false,"数据删除失败！");
		}	
	}
	
	@RequestMapping(value = "/resetPwd")
	@ResponseBody
	public Object resetPwd(StudentForm form) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		Integer id=form.getId();
		Student studnet=studentService.findById(id);
		studnet.setPassword(encoder.encode("1111"));
		studentService.save(studnet);
		return new AjaxResult("重置密码成功");
	}
	
	@RequestMapping(value = "/download")
	@ResponseBody
	public Object download(StudentForm form,HttpServletRequest request,HttpServletResponse response) {
		Integer id = form.getId();
		Student student=studentService.findById(id);
		String uuid=student.getUuid();
		String filename=student.getFilename();
		fileUtils.download(request, response, uuid, filename);
		return null;
	}
	@RequestMapping(value = "photo")
	public void photo(Integer id) {
		
	}
}
